package com.work.formaldehyde.activityadapter;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.work.formaldehyde.R;
import com.work.formaldehyde.activity.GouWuCheActivity;
import com.work.formaldehyde.activity.SetZhiFuBaoActivity;
import com.work.formaldehyde.model.GouWuCheModel;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.Url;

import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class GouWuCheAdapter extends BaseAdapter {
    public static ArrayList<GouWuCheModel.data> list;
    Activity c;
    Context context;
    public static  ArrayList<String> list1;
    ArrayList<Integer> lists;

    public GouWuCheAdapter(ArrayList<GouWuCheModel.data> list, Context context, ArrayList<String> list1, ArrayList<Integer> lists, Activity c) {
        // TODO Auto-generated constructor stub
        this.list = list;
        this.list1 = list1;
        this.lists = lists;
        this.context = context;
        this.c = c;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list != null ? list.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

//    init i;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        convertView = LayoutInflater.from(context).inflate(R.layout.gouwuche_list_item, null);
       final init i = new init();
        i.add_check = convertView.findViewById(R.id.add_check);
        i.gouwuche_img = convertView.findViewById(R.id.gouwuche_img);
        i.title_text = convertView.findViewById(R.id.title_text);
        i.gouwuche_name = convertView.findViewById(R.id.gouwuche_name);
        i.gouwuche_money = convertView.findViewById(R.id.gouwuche_money);
        i.gouwuche_manjian_text = convertView.findViewById(R.id.gouwuche_manjian_text);
        i.gouwuche_numtext = convertView.findViewById(R.id.gouwuche_numtext);
        i.gouwuche_jian = convertView.findViewById(R.id.gouwuche_jian);
        i.gouwuche_add = convertView.findViewById(R.id.gouwuche_add);
        i.gouwuche_name.setText(list.get(position).getGoods_name());
        ApiUtils.GET_IMAGE(context, list.get(position).getGoods_img(), i.gouwuche_img);
        double money = Double.parseDouble(list.get(position).getGoods_discount_price());
        double moneynum= Double.parseDouble(lists.get(position)+"");
        double moneyall = money *moneynum;
        String allmoneys = new DecimalFormat("0.00").format(moneyall);
        i.gouwuche_money.setText("¥" + allmoneys);
        if (list1.get(position).equals("2")) {
            GouWuCheActivity.gouwuchecheck++;
            i.add_check.setImageResource(R.mipmap.addc);
//            i.add_check.setImageDrawable(context.getResources().getDrawable(R.drawable.addcs));
        }
        i.gouwuche_numtext.setText(lists.get(position)+"");
        item_num=lists.get(position);
        ApiUtils.SetLog("num="+ item_num);
        i.gouwuche_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ApiUtils.isNetworkConnected(context)) {
                    item_num = lists.get(position);
                    positions = lists.get(position);
                    item_num++;
                    lists.set(position, item_num);
                    GouWuCheActivity.intary.set(position, item_num);
                    double money = Double.parseDouble(list.get(position).getGoods_discount_price());
                    double moneyall = money *item_num;
                    String allmoneys = new DecimalFormat("0.00").format(moneyall);
                    i.gouwuche_money.setText("¥" + allmoneys);
                    i.gouwuche_numtext.setText(item_num + "");
                    ApiUtils.windows(c, i.gouwuche_add, "请稍后");
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            ApiUtils.closepopup();
                        }
                    }, 10000);
                    okHttp_postFromParameters(position, 0);
                } else {
                    ApiUtils.SetToast(context, "请检查网络");
                }

            }
        });
        i.gouwuche_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ApiUtils.isNetworkConnected(context)) {
                    item_num = lists.get(position);
                    positions = lists.get(position);
                    if (item_num > 1) {
                        item_num--;
                        lists.set(position, item_num);
                        GouWuCheActivity.intary.set(position, item_num);
                        double money = Double.parseDouble(list.get(position).getGoods_discount_price());
                        double moneyall = money *item_num;
                        String allmoneys = new DecimalFormat("0.00").format(moneyall);
                        i.gouwuche_money.setText("¥" + allmoneys);
                        i.gouwuche_numtext.setText(item_num + "");
                        ApiUtils.windows(c, i.gouwuche_add, "请稍后");
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                ApiUtils.closepopup();
                            }
                        }, 10000);
                        okHttp_postFromParameters(position, 1);
                    }
                } else {
                    ApiUtils.SetToast(context, "请检查网络");
                }
            }
        });
        convertView.setTag(i);
        return convertView;
    }

    private int checkid = 0, positions;
   public static int item_num;

    int index = 1;


    public static class init {
        //首页list的子控件
        ImageView add_check, gouwuche_img;
        TextView title_text, gouwuche_name, gouwuche_money, gouwuche_manjian_text, gouwuche_numtext;
        RelativeLayout gouwuche_jian, gouwuche_add;
    }

    private void okHttp_postFromParameters(final int i, final int s) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    Request request;
                    if (s == 0) {
                        RequestBody formBody = new FormBody.Builder().add("uid", Url.USER_ID).add("logintoken", Url.LOGIN_TOKEN).add("num", "1").add("goodsid", list.get(i).getId())
                                .build();
                        request = new Request.Builder().url(Url.GOUWUCHEUP).post(formBody).build();
                    } else {
                        RequestBody formBody = new FormBody.Builder().add("uid", Url.USER_ID).add("logintoken", Url.LOGIN_TOKEN).add("num", "-1").add("goodsid", list.get(i).getId())
                                .build();
                        request = new Request.Builder().url(Url.GOUWUCHEUP).post(formBody).build();
                    }
                    okhttp3.Response response = okHttpClient.newCall(request).execute();
                    String msg = response.body().string();
                    Message message = new Message();
                    message.obj = msg;
                    gethot1.sendMessage(message);
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }
        }).
                start();
    }

    public Handler gethot1 = new Handler() {
        public void handleMessage(android.os.Message msg) {
            JSONObject success_Object;
            try {
                if (msg.obj != null) {
                    ApiUtils.closepopup();
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    if (code.equals("1")) {
                        for (int list = 0; list < GouWuCheActivity.new_list_views1.size(); list++) {
                            GouWuCheActivity.new_list_views1.set(list, "1");
                        }
                        GouWuCheActivity.myAdapter.notifyDataSetChanged();
                        GouWuCheActivity.allcheck_bl=0;
                        GouWuCheActivity. moneyall = 0;
                        GouWuCheActivity.allmoneys = new DecimalFormat("0.00").format(GouWuCheActivity.moneyall);
                        GouWuCheActivity.allmoney.setText("¥" + GouWuCheActivity.allmoneys);
                        GouWuCheActivity.check_gouwuche.setImageResource(R.mipmap.addnc);
                    } else {
//                        gouwuche_numtext.setText(positions + "");
                        ApiUtils.SetToast(context, success_Object.getString("msg"));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

}

